define([], function() {
    'use strict';
    /**
     * param-初始化的参数，function-实例拥有的方法
     * @param { el:"初始化upload的容器，建议用id", download:"点击下载模板的回调", emptyMsg:"未选择文件的提示语", clickText:"选择的文字" }
     * @function { clearFile:"清空选择的文件", getFile:"获取选择的文件信息" }
     */
    function Upload(o){
        if(!o.el){
            throw new Error("缺少容器id");
        }
        this.box = $(o.el);
        this.download = o.download;
        this.emptyMsg = o.emptyMsg || "请选择上传文件";
        this.clickText = o.clickText || "选择";
        this.downloadText = o.downloadText || "下载导入模板";
        this.init();
    }
    Upload.prototype.getTpl = function(){
        var str = '<div class="upload-box">'
            + '<div class="upload-main">'
            + '<div class="upload-main-show">'
            + '<a class="upload-main-click" href="javascript:void(0)">'+ this.clickText +'</a>'
            + '<span class="upload-filename empty">'+ this.emptyMsg +'</span>'
            + '<input type="file" class="upload-main-input" name="file" style="display: none;" />'
            + '</div>'
            + '<button class="upload-download-btn">'+ this.downloadText +'</button>'
            + '<span style="clear: both;"></span>'
            + '</div>'
            + '</div>';
        return str;
    }
    Upload.prototype.init = function(){
        this.box.html(this.getTpl());
        this.setListener();
    }
    Upload.prototype.clearFile = function(){
        this.box.find(".upload-main-input").val("");
        this.box.find(".upload-filename").text(this.emptyMsg).addClass("empty");
    }
    Upload.prototype.getFile = function(){
        return this.box.find(".upload-main-input")[0].files;
    }
    Upload.prototype.submit = function(url, callback, onProgress){
        var files = this.getFile();
        if(files.length <= 0){
            throw new Error("请选择上传文件");
        }
        var formdata = new FormData();
        for(var i = 0;i < files.length;i++){
            formdata.append('file', files[i]);
        }
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data:formdata,
            processData: false,
            contentType: false,
            xhr: function() {
                var xhr = new XMLHttpRequest();
                //使用XMLHttpRequest.upload监听上传过程，注册progress事件，打印回调函数中的event事件
                xhr.upload.addEventListener('progress', function (e) {
                    //loaded代表上传了多少, total代表总数为多少
                    var progressRate = (e.loaded / e.total) * 100 + '%';
                    onProgress(progressRate);
                })
                return xhr;
            },
            success:function(res){
                if(callback){
                    callback(res);
                }
            }
        })
    }
    Upload.prototype.setListener = function(){
        var _this = this;
        var btnClick = function(e){
            _this.box.find(".upload-main-input").click();
        }
        /**
         * 对选择点击事件的监听
         */
        _this.box.off("click", '.upload-main-click', btnClick);
        _this.box.on("click", '.upload-main-click', btnClick);

        var fileChange = function(e){
            var this_ = e.target;
            var file = this_.files[0];
            _this.box.find(".upload-filename").text(file.name).removeClass("empty");
        }
        /**
         * 对input-file的change事件进行监听
         */
        _this.box.off("change", '.upload-main-input', fileChange);
        _this.box.on("change", '.upload-main-input', fileChange);

        var downloadClick = function(e){
            if(_this.download){
                _this.download();
            }
        }
        /**
         * 对下载模板click进行监听
         */
        _this.box.off("click", '.upload-download-btn', downloadClick);
        _this.box.on("click", '.upload-download-btn', downloadClick);
    }
    return Upload;
});